﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Masood.MobileGames.SlidingLib.Abstract;
using Masood.MobileGames.SlidingLib.Factory;
using Masood.MobileGames.SlidingLib.Box;
using Microsoft.Xna.Framework.Audio;
using Masood.MobileGames.SlidingLib.DataObject;
using Masood.MobileGames.SlidingLib.Enum;


namespace Masood.MobileGames.Sliding
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        private AbstractGameBoard gameBoard;
        SoundEffect soundEffect;
        GameCreateOption gameCreationOption;

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            // TODO: use this.content to load your game content here
            string soundEffectAsset = "Windows Ding";
            soundEffect = contentManager.Load<SoundEffect>(soundEffectAsset);

            gameCreationOption = new GameCreateOption();
            gameCreationOption.NoOfRows = App.NoOfRows;
            gameCreationOption.NoOfCols = App.NoOfCols;
            gameCreationOption.SourceImageStream = App.SelectedImageStream;
            gameCreationOption.GraphicsDevice = SharedGraphicsDeviceManager.Current.GraphicsDevice;
            gameCreationOption.Content = this.contentManager;
            gameCreationOption.GameLevel = App.GameLevel;
            gameCreationOption.ActualWidth = this.ActualWidth;
            gameCreationOption.ActualHeight = this.ActualHeight;

            gameBoard = GameBoardFactory.CreateGame(gameCreationOption);
            gameBoard.NewGame();
            if (App.GameLevel == GameEnum.Hard)
            {
                gameBoard.Shuffle();
                gameBoard.Shuffle();
                gameBoard.Shuffle();
                gameBoard.Shuffle();
            }
            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            TouchCollection tc = TouchPanel.GetState();
            if (tc.Count > 0)
            {
                TouchLocation firstTouchLocation = tc[0];
                if (firstTouchLocation.State == TouchLocationState.Pressed)
                {
                    gameBoard.PerformSwap(firstTouchLocation);
                }
            }
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            gameBoard.Draw(spriteBatch);
        }
    }
}